package health.lm.com.shop.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * @author GQI
 * @description:
 * @date :2019-07-15 8:33
 */
public class AutoScrollRecyclerView extends RecyclerView {
    private Disposable mAutoTask = null;
    private int eventFlag;

    public AutoScrollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    //    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        switch (ev.getAction()){
//            case MotionEvent.ACTION_DOWN:
//                stop();
//                break;
//            case MotionEvent.ACTION_UP:
//                start();
//                break;
////            case MotionEvent.ACTION_UP:
//            default:
//                break;
//        }
//        return  super.dispatchTouchEvent(ev);
//    }
    public int y1,y2,x2,x1;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        switch (e.getAction()){
            case MotionEvent.ACTION_DOWN:
                x1 = (int) e.getX();
                y1 = (int) e.getY();
                break;
            case MotionEvent.ACTION_UP:
                x2 = (int) e.getX();
                y2 = (int) e.getY();
                int m = Math.abs(x1 - x2);
                if ( 6 > m) {
                    return false;// 距离较小，当作click事件来处理
                }
                if(60 < m){  // 真正的onTouch事件
                    start();
                    return true;
                }
                break;
            default:
                break;
        }

//        return false;
        return super.onInterceptTouchEvent(e);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()){
            case MotionEvent.ACTION_DOWN:
                stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                start();
                break;
            default:
                break;
        }
        return super.onTouchEvent(e);
    }

    public void start() {
        if (mAutoTask != null && !mAutoTask.isDisposed()) {
            mAutoTask.dispose();
        }
        mAutoTask = Observable.interval(1000, 50, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        smoothScrollBy(20, 0);
                    }
                });
    }

    public void stop() {
        if (mAutoTask != null && !mAutoTask.isDisposed()) {
            mAutoTask.dispose();
            mAutoTask = null;
        }
    }
}
